var $ = jQuery;

(function ($, Drupal, window, document, undefined) {
    Drupal.behaviors.my_custom_behavior = {
        attach: function (context, settings) {
            // auto upload file on file field change
            if ($.browser.msie){
                $('form input.form-file').attr('onchange', '_p(this)');
            }else{
                $('form', context).delegate('input.form-file', 'change', function(e) {
                    $(this).next('input[type="submit"]').mousedown();
                });
            }

            // icheck
            var $body = $('body');
            if (!$body.hasClass('page-lookbook') && !$body.hasClass('page-stylebook') && !$body.hasClass('page-msw')) {
                $('input').iCheck({
                    checkboxClass: 'icheckbox',
                    radioClass: 'iradio',
                });
            }

            $('input').on('ifToggled', function(event){
                $(this).change();
            });

            //experts page tabs swtich control
            $(".tabs-block .switch-btns").unbind("click").click(function() {
                var $active = $(".tabs-block.active");
                if($active.next(".tabs-block").length >= 1) {
                    $active.removeClass("active").hide().next(".tabs-block").addClass("active").show();
                } else {
                    $active.siblings(".first-tab.tabs-block").show().addClass("active");
                    $active.hide().removeClass("active");
                }
            });

        }
    };

    var page_width = $(window).width();
    var page_height = $(window).height();


    $(document).ready(function () {
        //Placeholder & its Compability to ie8
        $('form input[type=text], form input[type=email], form input[type=password], form textarea').each(function () {
            var input = $(this);
            if (input.is('.block-search .form-item-search-block-form input.form-text') || input.is('.region-help .views-widget-filter-combine .form-text')) {
                input.attr("placeholder", "Search keywords...");
            }
        });
        if ($.browser.msie && $.browser.version <= 9) {
            $("[placeholder]").focus(function () {
                if ($(this).val() == $(this).attr("placeholder") || $(this).parent().hasClass("placeholder")) {
                    $(this).val("");

                    if ($(this).hasClass("password-field") || $(this).hasClass("password-confirm")) {
                        $(this).parent().removeClass("placeholder");
                    }
                }
            }).blur(function () {
                if ($(this).val() == "") {
                    $(this).val($(this).attr("placeholder"));

                    if ($(this).hasClass("password-field") || $(this).hasClass("password-confirm")) {
                        $(this).parent().addClass("placeholder");
                        $(this).val("");
                    }
                }
            }).blur();

            $("[placeholder]").parents("form").submit(function () {
                $(this).find('[placeholder]').each(function () {
                    if ($(this).val() == $(this).attr("placeholder")) {
                        $(this).val("");
                    }
                })
            });
        }

		//lotfy xmass function
		$("body.mobile.section-christmas-2015 #page #main .jessica_xmas_subcat_main_ad").insertAfter("body.mobile.section-christmas-2015 #page #main #content .wrapper .jessica_xmas_subcat_views .wrapper .view-content .views-row-2");

		// lotfy jtv menu functions
        /*var hideSublist = $('body.page-category-jtv .jtv-menu-mobile .wrapper ul li ul').hide();
        $('body.page-category-jtv .jtv-menu-mobile .wrapper > ul.menu > li span').click(function() {
            $('body.page-category-jtv .jtv-menu-mobile .wrapper ul ul').slideToggle(300);
        });*/


        // Search block
        $("form[action='/search-page']").addClass("search-block").appendTo("#header > .wrapper");

        $("#header .search-trigger").click(function() {
            $("form.search-block").fadeToggle(500, function() {
                $(this).find(".form-text").focus();
            });
        })
        $("form.search-block .form-text").blur(function() {
            $("form.search-block").fadeOut(500);
        });
        $("form.search-block .form-text").attr("placeholder", "SEARCH...");


        // Menu trigger toggle

        $(".menu-trigger").click(function() {
            $("body").toggleClass("menu-on");
        });
        $(".nav-backdrop").click(function() {
            $("body").removeClass("menu-on");
            $(".category-menu.expanded").removeClass(".expanded");
        });
        $(".remaining_load").on("click", function(e){
            e.preventDefault();
            $(this).hide();
            $("#block-views-nodequeue-1-block-3").show();
        })


        // Fluid image BG STYLE

        $(".fluid-image[bgimage]").each(function() {

            if ($(this).next(".image-path").length >= 1) {
                var url = "url(" + $(this).next(".image-path").find("img").attr("src") + ")";
            } else {
                var url = "url(" + $(this).attr("bgimage") + ")";
            }
            $(this).attr("style","background-image: " + url).attr("bgimage","");;
        })


        // Text size toogle
        $(".font-size-trigger").click(function() {
            $(this).toggleClass("active");
            $("body").toggleClass("font-size-trigger-on");
        })

        // Move Ad-block to appropriate location
        $("body.page-node .adblock-300x300-top").insertAfter($(".header-title"));

        // disable colorbox and change the link to node page in mobile
        $(".colorbox[data-node]").each(function(i,v) {
            $(v).attr("href", $(v).data("node"));
            $(v).removeClass("colorbox-load colorbox init-colorbox-load-processed cboxElement");
        });
    });

    $(window).load(function () {
        $(".messages").dblclick(function () {
            if (!$(".krumo-root").length) {
                $(this).fadeOut(500);
            }
        })

        //Sticky Header
        var $sticky_object = $("#header");
        $sticky_object.addClass("sticky");

        // Adding Placeholder for non-label form items
        $("form label").each(function() {

            if($(this).css("display") == "none") {
                var $text = $(this).text();
                $(this).parent(".form-item").find(".form-text").attr("placeholder",$text);
            }
        })


        // Mobile menu first level item behavior
        // $(".nav-menu li.is-expanded > .menu__link").on('click tap', function(e) {
        $("#nav-menu li.menuparent > a").on('click mousedown', function(e) {
            if (!$(this).parent().hasClass("over")) {
                e.preventDefault();
            }

            if (e.type === "click") {
                $(".menu .menu").css("display", "none");
                $(this).parent().find(".menu").css('display', 'block');
            }
         })


        //experts page tabs swtich control
        var $first = 0;
        $(".tabs-block").each(function() {
            $first++;
            if ($first != 1) {
                $(this).hide();
            } else {
                $(this).addClass("active first-tab");
            }
        });

        // prevent ios safari overscroll the sidemenu (#nav-menu)
        // by Kelvin
        var overscroll = function(el) {
            el.addEventListener('touchstart', function(evt) {
                var top = el.scrollTop
                    , totalScroll = el.scrollHeight
                    , currentScroll = top + el.offsetHeight
                    //If we're at the top or the bottom of the containers
                    //scroll, push up or down one pixel.
                    //
                    //this prevents the scroll from "passing through" to
                    //the body.
                    if(top === 0) {
                        el.scrollTop = 1
                    } else if(currentScroll === totalScroll) {
                        el.scrollTop = top - 1
                    }
            })
            el.addEventListener('touchmove', function(evt) {
                //if the content is actually scrollable, i.e. the content is long enough
                //that scrolling can occur
                if(el.offsetHeight < el.scrollHeight)
                    evt._isScroller = true
            })
        }
        overscroll(document.querySelector('#nav-menu'));

        $("#nav-backdrop").on("touchmove", function(e) {
            e.preventDefault();
        })

    })

})(jQuery, Drupal, this, this.document);


function _p(elem){
    if ($(elem).val().length > 0){
        $(elem).next('input[type="submit"]').mousedown();
    }
}

